package gui;

import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.ButtonGroup;
import javax.swing.SwingConstants;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.border.LineBorder;
import javax.swing.JToggleButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

import BL.AutoMgmt;
import BL.PneuMgmt;
import BusinessObjects.Auto;
import BusinessObjects.Pneu;

public class ErfassenGUI extends JFrame implements ActionListener{

	private JPanel contentPane;
	private JTextField txtModell;
	private JTextField txtMarke;
	private JTextField txtLeistung;
	private JTextField txtNeuOccOld;
	private JTextField txtTyp;
	private JTextField txtKMStand;
	private JTextField txtFarbe;
	private JTextField txtVerbrauch;
	private JLabel lblLeistung;
	private JLabel lblKMStand;
	private JLabel lblVerbrauch;
	private JTextField txtPreis;
	private JPanel panePneu;
	private JLabel lblMarkePneu;
	private JTextField txtPneuMarke;
	private JLabel lblDimmensionPneu;
	private JTextField txtPneuDim;
	private JLabel lblSommerWinterPneu;
	private JTextField txtPneuSommerWinter;
	private JLabel lblAblaufdatumPneu;
	private JTextField txtPneuAblaufdat;
	private JLabel lblErstEinsatzPneu;
	private JLabel lblneuOccOld;
	private JTextField txtPneuErstEinsatz;
	private JTextField txtPneuPreis;
	
	private JPanel panelAuto;
	private JButton btnAbbrechen;
	private JButton btnOK;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ErfassenGUI frame = new ErfassenGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ErfassenGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(0, 102, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("Erfassen");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("Lucida Grande", Font.PLAIN, 64));
		label.setBounds(23, 6, 405, 64);
		contentPane.add(label);
		
		panelAuto = new JPanel();
		panelAuto.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelAuto.setBounds(33, 97, 391, 152);
		contentPane.add(panelAuto);
		panelAuto.setLayout(new GridLayout(9, 2, 0, 0));
		panelAuto.setVisible(true);
		
		JLabel lblMarke = new JLabel("Marke");
		panelAuto.add(lblMarke);
		
		
		txtMarke = new JTextField();
		panelAuto.add(txtMarke);
		txtMarke.setColumns(10);
		
		
		JLabel lblTyp = new JLabel("Typ");
		panelAuto.add(lblTyp);
		
		txtTyp = new JTextField();
		panelAuto.add(txtTyp);
		txtTyp.setColumns(10);
		
		lblneuOccOld = new JLabel("NeuOccOld");
		panelAuto.add(lblneuOccOld);
		
		txtNeuOccOld = new JTextField();
		panelAuto.add(txtNeuOccOld);
		txtNeuOccOld.setColumns(10);
		
		JLabel lblModell = new JLabel("Modell");
		panelAuto.add(lblModell);
		
		txtModell = new JTextField();
		panelAuto.add(txtModell);
		txtModell.setColumns(10);
		
		JLabel lblFarbe = new JLabel("Farbe");
		panelAuto.add(lblFarbe);
		
		txtFarbe = new JTextField();
		panelAuto.add(txtFarbe);
		txtFarbe.setColumns(10);
		
		lblKMStand = new JLabel("KM-Stand ");
		panelAuto.add(lblKMStand);
		
		txtKMStand = new JTextField();
		panelAuto.add(txtKMStand);
		txtKMStand.setColumns(10);
		
		lblLeistung = new JLabel("Leistung in PS");
		panelAuto.add(lblLeistung);
		
		txtLeistung = new JTextField();
		panelAuto.add(txtLeistung);
		txtLeistung.setColumns(10);
		
		lblVerbrauch = new JLabel("Durchschnitssverbrauch");
		panelAuto.add(lblVerbrauch);
		
		txtVerbrauch = new JTextField();
		panelAuto.add(txtVerbrauch);
		txtVerbrauch.setColumns(10);
		
		JLabel lblPreis = new JLabel("Preis");
		panelAuto.add(lblPreis);
		
		txtPreis = new JTextField();
		panelAuto.add(txtPreis);
		txtPreis.setColumns(10);
		
		ButtonGroup grp = new ButtonGroup();
		
		JToggleButton tglbtnAuto = new JToggleButton("Auto");
		tglbtnAuto.addActionListener(this);
		tglbtnAuto.setBounds(62, 68, 161, 29);
		contentPane.add(tglbtnAuto);
		
		JToggleButton tglbtnPneu = new JToggleButton("Pneu");
		tglbtnPneu.addActionListener(this);
		tglbtnPneu.setBounds(235, 68, 161, 29);
		contentPane.add(tglbtnPneu);
		
		grp.add(tglbtnAuto);
		grp.add(tglbtnPneu);
		
		
		panePneu = new JPanel();
		panePneu.setBounds(33, 97, 391, 152);
		contentPane.add(panePneu);
		panePneu.setLayout(new GridLayout(6, 2, 0, 0));
		panePneu.setVisible(false);
		
		lblMarkePneu = new JLabel("Marke");
		panePneu.add(lblMarkePneu);
		
		txtPneuMarke = new JTextField();
		panePneu.add(txtPneuMarke);
		txtPneuMarke.setColumns(10);
		
		lblDimmensionPneu = new JLabel("Dimmension");
		panePneu.add(lblDimmensionPneu);
		
		txtPneuDim = new JTextField();
		panePneu.add(txtPneuDim);
		txtPneuDim.setColumns(10);
		
		lblSommerWinterPneu = new JLabel("Sommer/Winter");
		panePneu.add(lblSommerWinterPneu);
		
		txtPneuSommerWinter = new JTextField();
		panePneu.add(txtPneuSommerWinter);
		txtPneuSommerWinter.setColumns(10);
		
		lblAblaufdatumPneu = new JLabel("Ablaufdatum");
		panePneu.add(lblAblaufdatumPneu);
		
		txtPneuAblaufdat = new JTextField();
		panePneu.add(txtPneuAblaufdat);
		txtPneuAblaufdat.setColumns(10);
		
		lblErstEinsatzPneu = new JLabel("Erst Einsatz");
		panePneu.add(lblErstEinsatzPneu);
		
		txtPneuErstEinsatz = new JTextField();
		panePneu.add(txtPneuErstEinsatz);
		txtPneuErstEinsatz.setColumns(10);
		
		JLabel lblPreisPneu = new JLabel("Preis");
		panePneu.add(lblPreisPneu);
		
		txtPneuPreis = new JTextField();
		panePneu.add(txtPneuPreis);
		txtPneuPreis.setColumns(10);
		
		btnAbbrechen = new JButton("<--");
		btnAbbrechen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new StartGUI().setVisible(true);
				dispose();
			}
		});
		btnAbbrechen.setBounds(0, 27, 50, 29);
		contentPane.add(btnAbbrechen);
		
		btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(panelAuto.isVisible())
				{
					Auto a = new Auto(txtMarke.getText(), txtModell.getText(), txtTyp.getText(), txtNeuOccOld.getText(), txtKMStand.getText(), txtFarbe.getText(), txtLeistung.getText(), txtVerbrauch.getText(), txtPreis.getText());
					AutoMgmt.addAuto(a);
				}
				else if(panePneu.isVisible())
				{
					Pneu p = new Pneu(txtPneuMarke.getText(), txtPneuSommerWinter.getText(), txtPneuDim.getText(), txtPneuAblaufdat.getText(), txtPneuErstEinsatz.getText(), txtPneuPreis.getText());
					PneuMgmt.addPneu(p);
				}
				
			}
		});
		btnOK.setBounds(279, 249, 117, 29);
		contentPane.add(btnOK);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if(event.getActionCommand().equals("Auto"))
		{
			panelAuto.setVisible(true);
			panePneu.setVisible(false);
			
			
		} else if(event.getActionCommand().equals("Pneu"))
		{
			panelAuto.setVisible(false);
			panePneu.setVisible(true);
		}
		
	}
}
